<template>
    <div class="light-Ct"
        :class="{ 'light-Ct-line': parentChildrenLength && parentChildrenLength > 1 || (parentChildrenLength && parentChildrenLength > 2 && item.children && item.children.length > 2) }">
        <div class="item-Cd" :class="{ 'item-Cd-line': item.children && item.children.length > 0 }">
            <div class="item-Cd-title">
                <el-popover placement="left" :width="200" trigger="hover">
                    <template #reference>
                        <div style="display: flex;align-items: center;">
                            <div class="light-circle" style="position: relative;" :class="{
                                'light-green': item.nodeState == 1,
                                'light-red': item.nodeState == 0,
                                'light-orange': item.nodeState == 2,
                            }">
                                <span class="circleTit"> {{ item.percent }} </span>
                            </div>
                            <div class="item-name">
                                <div v-if="!isEditing" @click="startEdit()" class="pointers"
                                    style="display: inline-block;">{{
                                        item.nodeName }}</div>
                                <el-input v-else v-model="editedName" @blur="finishEdit" @keyup.enter="finishEdit"
                                    ref="editInput" style="width: 150px;" />
                            </div>
                        </div>
                    </template>
                    <OperationButtons :index="index" :item="item" @add-brother-item="addBrotherItem"
                        @add-child-item="addChildItem" @edit-item="testIntem" @delete-item="deleteItem"
                        @toggle-expand="expandCollapse" @seeNode-Detail="seeNodeDetail" />
                </el-popover>
            </div>
        </div>

        <div style="display: flex;align-items: center;justify-content: center;">
            <div v-if="item.expanded && item.children && item.children.length"
                :class="item.children && item.children.length ? 'childItem childItemSon' : 'childItem'">
                <!-- :style="parentChildrenLength && parentChildrenLength == 1 || item.children && item.children.length == 0 ? 'display: block;' : ''" -->
                <RecursiveItemHX v-for="(child, childIndex) in item.children" :key="childIndex" :item="child"
                    :index="childIndex" @editItem="editItem" @addBrotherItem="addBrotherItem"
                    @addChildItem="addChildItem" @deleteItem="deleteItem" @testIntem="testIntem"
                    @seeNodeDetail="seeNodeDetail" @expandCollapse="expandCollapse"
                    :parentChildrenLength="item.children.length" />
            </div>

        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, defineProps, defineEmits, onUnmounted, nextTick, watch } from 'vue';
import RecursiveItemHX from './RecursiveItemHX';
import OperationButtons from './OperationButtons.vue';

const props = defineProps({
    item: {
        type: Object,
        required: true
    },
    index: {
        type: Number,
        required: true
    },
    parentChildrenLength: { // 新增 prop
        type: Number,
        default: 0
    },
    popoverPlacement: {
        type: String,
        default: 'right'
    }
});

const emit = defineEmits(['testIntem', 'editItem', 'addBrotherItem', 'addChildItem', 'deleteItem', 'seeNodeDetail', 'expandCollapse']);

const isEditing = ref(false);
const editedName = ref(props.item.nodeName);
const editInput = ref(null);

watch(() => props.item, (newItem) => {
    if (newItem) {
        editedName.value = newItem.nodeName;
    }
},
    { immediate: true }
);
const startEdit = () => {
    isEditing.value = true;
    nextTick(() => {
        if (isEditing.value && editInput.value) {
            editInput.value.focus();
        }
    });
};

const finishEdit = () => {
    isEditing.value = false;
    if (!editedName.value) {
        return; // ElMessage.error("请输入节点名称");
    }
    if (editedName.value !== props.item.nodeName) {
        emit('editItem', props.index, { ...props.item, nodeName: editedName.value });
    }
};

const testIntem = (index, item) => {
    emit('testIntem', index, item);
};

const editItem = (index, item) => {
    emit('editItem', index, item);
};

const addBrotherItem = (index, item) => {
    emit('addBrotherItem', index, item);
};

const addChildItem = (index, item) => {
    emit('addChildItem', index, item);
};

const deleteItem = (index, item) => {
    emit('deleteItem', index, item);
};

const seeNodeDetail = (index, item) => {
    emit('seeNodeDetail', index, item)
};

const expandCollapse = (index, item) => {
    emit('expandCollapse', index, item);
};

onMounted(() => {

});

onUnmounted(() => {
    editInput.value = null;
});

</script>

<style lang="scss" scoped>
.pointers {
    cursor: pointer;
}

// 纵向样式
.light-item {

    .light-Ct {
        margin: 2px 0px;
        display: flex;
        align-items: center;

        .item-Cd {
            padding: 4px 8px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #f9f9f9;
            transition: background-color 0.3s;
            position: relative;

            &.item-Cd-line::after {
                content: "";
                position: absolute;
                right: -25px;
                top: 50%;
                width: 25px;
                height: 1px;
                background-color: rgb(43, 38, 38);
            }

            &:hover {
                background-color: #eee;
            }

            .item-Cd-title {
                display: flex;
                align-items: center;

                .light-circle {
                    width: 12px;
                    height: 12px;
                    line-height: 30px;
                    text-align: center;
                    border-radius: 50%;
                    margin-right: 4px;
                    color: white;

                    &.light-green {
                        background-color: green;
                    }

                    &.light-red {
                        background-color: red;
                    }

                    &.light-orange {
                        background-color: orange;
                    }

                    .circleTit {
                        position: absolute;
                        top: -40px;
                        left: 0px;
                        font-weight: bold;
                        color: black;
                    }
                }
            }

            .operation {
                margin-left: 22px;
                margin-top: 10px;
                display: flex;

                a {
                    color: var(--global-link-color);
                    text-decoration: none;
                    cursor: pointer;
                }

                span {
                    margin: 0px 4px;
                    color: #ccc;
                }
            }

            .item-name {
                color: var(--global-font-color);
                // font-size: var(--global-font-size);
                font-size: 12px;
                width: calc(100% - 16px);
            }
        }

        .childItem {
            margin-left: 24px;
            position: relative;

            .light-Ct {
                position: relative;
            }


            .light-Ct-line::after {
                content: "";
                position: absolute;
                left: -24px;
                top: 0px;
                width: 1px;
                height: calc(100% + 2px);
                background-color: rgb(43, 38, 38);
            }

            >.light-Ct-line:first-child::after {
                content: "";
                position: absolute;
                left: -24px;
                top: 50%;
                width: 1px;
                height: calc(50% + 2px);
                background-color: rgb(43, 38, 38);
            }

            >.light-Ct-line:last-child::after {
                content: "";
                position: absolute;
                left: -24px;
                top: 0px;
                width: 1px;
                height: 50%;
                background-color: rgb(43, 38, 38);
            }
        }

        .childItemSon {
            padding-left: 24px;

            &>.light-Ct::before {
                content: "";
                position: absolute;
                left: -24px;
                top: 50%;
                width: 24px;
                height: 1px;
                background-color: black;
            }
        }
    }
}

.item-info {
    text-align: left;
    line-height: 20px;
    color: var(--global-font-color);
    font-size: var(--global-font-size);

    p {
        margin: 0px;
    }
}

.light-wrapHXRL {

    .item-name .pointers,
    .circleTit,
    .el-input {
        transform: rotate(180deg) scaleY(-1);
    }
}
</style>
